/*
 * Step_One.cpp
 *
 *  Created on: 2011-6-17
 *      Author: ryan
 */

#include "Step_One.h"

int analizeOpt(int argc, char** argv){
	int ch, m = 0;
	while((ch = getopt(argc, argv, "I:R:O:h")) != -1){
		switch(ch){
			case 'I':	strcpy(inputImageFileName, optarg);	++m;	break;
			case 'R':	strcpy(refImageFileName, optarg);		++m;	break;
			case 'O':	strcpy(outFilePath, optarg);			++m;	break;
			default:	break;
		}
	}
	return m;
}

void fileReading() {
	if (strcmp(refImageFileName, "") != 0) {
		printf("Reading Reference Image..............");
		ReadBmp(refImageFileName, &ref);
		printf("End!\t With Image Size (%d X %d)\n", ref.width, ref.height);
	} else {
		fprintf(stderr, "Please Give a Reference Image!");
		return;
	}
	if (strcmp(inputImageFileName, "") != 0) {
		printf("Reading Input Image..................");
		ReadBmp(inputImageFileName, &inp);
		printf("End!\t With Image Size (%d X %d)\n", inp.width, inp.height);
	} else {
		fprintf(stderr, "Please Give a Input Image!\n");
		return;
	}
	printf("\n");
}

void getImage(KEY_VALUE *kv) {
	int i, size;
	unsigned char *q = kv->value;
	size = sizeof(int);
	memcpy(&ref.width, q, size);
	q += size;
	memcpy(&ref.height, q, size);
	q += size;
	memcpy(&inp.width, q, size);
	q += size;
	memcpy(&inp.height, q, size);
	q += size;

	ref.data = malloc(sizeof(color*) * ref.width);
	size = sizeof(color) * ref.height;
	for (i = 0; i < ref.width; ++i) {
		ref.data[i] = malloc(size);
		memcpy(ref.data[i], q, size);
		q += size;
	}
	inp.data = malloc(sizeof(color*) * inp.width);
	size = sizeof(color) * inp.height;
	for(i = 0; i < inp.width; ++i){
		inp.data[i] = malloc(size);
		memcpy(inp.data[i], q, size);
		q += size;
	}
}

void mirror(Image* inp, Image* out){
	int i;
	out->width = inp->width;
	out->height = inp->height;
	out->data = malloc(sizeof(color*) * inp->width);
	for(i = 0; i < inp->width; ++i){
		out->data[i] = malloc(sizeof(color) * inp->height);
		memset(out->data[i], 0, sizeof(color) * inp->height);
	}
}
